//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.stock.log;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import javax.swing.*;
import org.jpgolay.gui.fixed.SearchBox;
import org.jpgolay.gui.fixed.TableViewer;
import org.jpgolay.me.list.ListPrint;
import org.jpgolay.me.persistence.SQLList;
import org.jpgolay.me.util.*;

// Referenced classes of package org.jpgolay.me.stock.log:
//            LogStockListPanel
public class LogStockListController {

    public LogStockListController(LogStockListPanel logStockListPanel, Parameter param, JDesktopPane desktop) {
        this.logStockListPanel = logStockListPanel;
        this.param = param;
        this.desktop = desktop;
        sqlList = new SQLList();
        date = new MEDate();
        initBatchListPanel();
    }

    private void initBatchListPanel() {
        logStockListPanel.getSearch().setVal("");
        logStockListPanel.getSearch().setList(new String[]{
                    "N\260 LOT", "CODE ART", "OPERATION", "DATE"
                });
        logStockListPanel.getSearch().setSelectedIndex(0);
        logStockListPanel.getSearch().getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        logStockListPanel.getSearch().getTextField().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSearch();
            }
        });
        logStockListPanel.getPrintLabel().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionPrint();
            }
        });
        logStockListPanel.getTable().setTypes(new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.math.BigDecimal.class, java.lang.String.class, java.lang.String.class,
                    java.lang.String.class
                });
        logStockListPanel.getTable().setLastColumnNotEditable(0);
        logStockListPanel.getTable().setCellEditable(false);
        logStockListPanel.getTable().setAlternateColor(MEUtility.LOG_STOCK_LINE);
        logStockListPanel.getTable().setColumnNames(new String[]{
                    "Id", "Date", "Op\351ration", "De", "Vers", "Id Lot", "Article", "Quantit\351", "Note", "User",
                    "Modifi\351"
                });
        logStockListPanel.getTable().setPreferredColumnSize(new int[]{
                    20, 60, 60, 60, 60, 60, 60, 50, 100, 60,
                    60
                });
        actionSearch();
    }

    private Object[][] getDataEmpty() {
        Object data[][] = new Object[3][11];
        for (int i = 0; i < data.length; i++) {
            data[i][0] = null;
            if (i == 1) {
                data[i][1] = "AUCUN";
                data[i][2] = "EVENEMENT";
            } else {
                data[i][1] = null;
                data[i][2] = null;
            }
            data[i][3] = null;
            data[i][4] = null;
            data[i][5] = null;
            data[i][6] = null;
            data[i][7] = null;
            data[i][8] = null;
            data[i][9] = null;
            data[i][10] = null;
        }

        return data;
    }

    private void actionSearch() {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT id,date,operation,de,vers,no_lot,articles_id,quantite,commentaire,update_user_id,update_time FROM journal_stock");
        switch (logStockListPanel.getSearch().getSelectedIndex()) {
            case 0: // '\0'
                if (logStockListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY no_lot");
                } else {
                    sql.append(" WHERE no_lot=");
                    sql.append(logStockListPanel.getSearch().getVal());
                    sql.append("' ORDER BY no_lot");
                }
                break;

            case 1: // '\001'
                if (logStockListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY articles_id");
                } else {
                    sql.append(" WHERE articles_id LIKE '");
                    sql.append(logStockListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY articles_id");
                }
                break;

            case 2: // '\002'
                if (logStockListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY operation");
                } else {
                    sql.append(" WHERE operation LIKE '");
                    sql.append(logStockListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY operation");
                }
                break;

            case 3: // '\003'
                if (logStockListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY date");
                } else {
                    sql.append(" WHERE date LIKE '");
                    sql.append(logStockListPanel.getSearch().getVal());
                    sql.append("%' ORDER BY date");
                }
                break;

            default:
                if (logStockListPanel.getSearch().getVal().equals("")) {
                    sql.append(" ORDER BY id");
                } else {
                    sql.append(" WHERE id=");
                    sql.append(logStockListPanel.getSearch().getVal());
                    sql.append(" ORDER BY id");
                }
                break;
        }
        Object data[][] = (Object[][]) null;
        if (sqlList.search(sql.toString())) {
            data = sqlList.getRowsData();
            for (int i = 0; i < data.length; i++) {
                data[i][1] = date.toME((String) data[i][1]);
                data[i][7] = (new BigDecimal((String) data[i][7])).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue());
            }

        }
        if (data != null) {
            logStockListPanel.getTable().setData(data);
            logStockListPanel.getSearch().setVal("");
        } else {
            logStockListPanel.getTable().setData(getDataEmpty());
            JOptionPane.showMessageDialog(null, "Aucune donn\351es de correspondent \340 votre recherche !");
        }
    }

    private void actionPrint() {
        String columnName[] = {
            "col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8", "col9", "col10",
            "col11"
        };
        Object data[][] = logStockListPanel.getTable().getData();
        ListPrint listPrint = new ListPrint(data, columnName, "logStockList", true, param);
    }
    private LogStockListPanel logStockListPanel;
    private Parameter param;
    private JDesktopPane desktop;
    private SQLList sqlList;
    private MEDate date;
}
